package com.sheep.classroompie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sheep.classroompie.entity.Student;
import com.sheep.classroompie.entity.Teacher;
import com.sheep.classroompie.service.StudentService;
import com.sheep.classroompie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin("http://localhost:8080")
public class StudentController {
  @Autowired
  StudentService studentService;
  @RequestMapping("/login")
  @ResponseBody
  public R<Student> login(@RequestBody Student student){
    // 根据用户名查询用户信息
    LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Student::getUsername, student.getUsername());
    Student studentOne = studentService.login(student);
    // 如果查询不到用户或者密码不正确，返回错误信息
    if(studentOne == null || !studentOne.getPassword().equals(student.getPassword())){
      return R.error("用户名或密码错误");
    }
    return R.success(studentOne);
  }

  @RequestMapping("/register")
  @ResponseBody
  public R<Student> register(@RequestBody Student student){
    Student studentOne = new Student(student.getStuId(),student.getUsername(),student.getPassword(),student.getIdentity(),student.getSchool(),student.getName(), student.getProfilePicture());
    if(studentService.register(studentOne) != 0){
      return R.success(student);
    }
    return R.error("注册信息错误!");
  }
  @RequestMapping("/uploadAvatar")
  @ResponseBody
  public Boolean uploadAvatar(@RequestBody Student student){
    return studentService.uploadAvatar(student);
  }
  @RequestMapping("/changePassword")
  @ResponseBody
  public void changePassword(@RequestBody Student student){
    studentService.changePassword(student);
  }
}
